//
//  PartyAnimalAppDelegate.m
//  PartyAnimal
//
//  Created by Hilary Muludiang on 3/12/12.
//  Copyright 2012 Versatile Systems, Inc. All rights reserved.
//

#import "PartyAnimalAppDelegate.h"
#import "PartyAnimalNavigatorViewController.h"
#import "StackMob.h"
#import "iBacchanal.h"

#define kHostName @"m.google.com"

#define TESTING 0


@implementation PartyAnimalAppDelegate

@synthesize window;
@synthesize partyAnimalTabBar;
@synthesize partyAnimalNavigatorViewController;
@synthesize hostReach, internetReach;

-(void)checkStatus:curReach{
	iBacchanal *bacchanal = [iBacchanal sharedUser];
	NetworkStatus netStatus = [curReach currentReachabilityStatus];
	switch (netStatus){
		case NotReachable:{
			//show an alert to let the user know that they can't connect...
			UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message: bacchanal.downMessage
														   delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
			[alert show];
			bacchanal.networkUp = NO;
			bacchanal.stackmobLoaded = NO;
			NSLog(@"No session...");
			break;
		}
		default:{
			// Network is reachable
			bacchanal.networkUp = YES;
			bacchanal.stackmobLoaded = YES;
			[[StackMob stackmob] startSession];
			NSLog(@"sessions loaded..");
			break;
		}
    }
}

- (void)createAdBannerView {
	iBacchanal *bacchanal = [iBacchanal sharedUser];
    Class classAdBannerView = NSClassFromString(@"ADBannerView");
    if (classAdBannerView != nil) {
        bacchanal.adBannerView = [[[classAdBannerView alloc] 
								   initWithFrame:CGRectZero] autorelease];
        [bacchanal.adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects: 
																   ADBannerContentSizeIdentifierPortrait, 
																   ADBannerContentSizeIdentifierLandscape, nil]];
        if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
            [bacchanal.adBannerView setCurrentContentSizeIdentifier:
			 ADBannerContentSizeIdentifierLandscape];
        } else {
            [bacchanal.adBannerView setCurrentContentSizeIdentifier:
			 ADBannerContentSizeIdentifierPortrait];            
        }
        [bacchanal.adBannerView setFrame:CGRectOffset([bacchanal.adBannerView frame], 0, 
													  -[bacchanal getBannerHeight])];
        [bacchanal.adBannerView setDelegate:self];
		
       //[self.partyAnimalTabBar addSubview:bacchanal.adBannerView];        
    }
}


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
	[self createAdBannerView];
    internetReach = [[Reachability reachabilityForInternetConnection] retain];
	[internetReach startNotifier];
	[self checkStatus:internetReach];
	
	//hostReach = [[Reachability reachabilityWithHostName: kHostName] retain];
	//[hostReach startNotifier];
	//[self updateStatus: hostReach];
	
	[TestFlight takeOff:@"3fce96f49bd5ef14148053e0e35378b3_NzUxNjMyMDEyLTAzLTI2IDEzOjUzOjA1Ljk5OTI3Mg"];
	
#if TESTING
	NSLog(@"%@", [[UIDevice currentDevice] uniqueIdentifier]);
	[TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#endif

    // Override point for customization after application launch.
    [window addSubview:partyAnimalTabBar.view];
    [self.window makeKeyAndVisible];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
	[[StackMob stackmob] endSession];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
	[hostReach release];
	[internetReach release];
	[partyAnimalNavigatorViewController release];
	[partyAnimalTabBar release];
    [window release];
    [super dealloc];
}


@end
